package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RequestMapping("/setmeal")
@RestController
public class SetMealController {
    
    @Autowired
    private SetMealService setMealService;
    
    
    /**
     * 根据分类id和装填来查询套餐数据
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId , Integer status){
        
        //调用service
        List<Setmeal> list = setMealService.findCategoryById(categoryId , status);
        
        return R.success(list);
    }
    
    /**
     * 删除套餐（包含单个删除和批量删除）
     * @param ids
     * @return
     */
//    public R delete(long [] ids){
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        
        int row = setMealService.delete(ids);
        if(row >0){
            return R.success("删除成功~！");
        }else{
            return R.success("删除失败~！");
        }
    
    }
    
    /**
     * 分页查询套餐
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        
        //1. 调用service
        IPage<SetmealDto> page = setMealService.findPage(pageParam);
        
        //2. 返回
        return R.success(page);
    
    }
    
    
    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        
        //1. 调用service
        int row = setMealService.add(setmealDto);
        
        //2. 判断
        if(row > 0 ){
            return R.success("新增套餐成功！");
        }else{
            return R.error("新增套餐失败！");
        }
        
    }
    @PostMapping("/status/{status}")
    public R updateSatus(@RequestParam List<Long> ids, @PathVariable Integer status){
       int rows = setMealService.updateStatus(ids,status);
       if (rows>0){
           return R.success("修改成功！");
       }else{
           return R.error("修改失败！");
       }
    }
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
       int rows = setMealService.upadateSetmeal(setmealDto);
        if (rows>0){
            return R.success("修改成功！");
        }else{
            return R.error("修改失败！");
        }
    }
}
